Nuevamente, vamos a leer primero unos datos...
In [ ]:
# primero hacemos los imports de turno
import os
import datetime as dt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
np.random.seed(19760812)
plt.style.use('bmh')
%matplotlib inline
In [ ]:
# Leemos los datos del fichero 'mast.txt'
ipath = os.path.join('Datos', 'mast.txt')
def dateparse(date, time):
YY = 2000 + int(date[:2])
MM = int(date[2:4])
DD = int(date[4:])
hh = int(time[:2])
mm = int(time[2:])
return dt.datetime(YY, MM, DD, hh, mm, 0)
cols = ['Date', 'time', 'wspd', 'wspd_max', 'wdir',
'x1', 'x2', 'x3', 'x4', 'x5',
'wspd_std']
wind = pd.read_csv(ipath, sep = "\s*", names = cols,
parse_dates = {'Timestamp': [0, 1]}, index_col = 0,
date_parser = dateparse)
In [ ]:
# Leemos los datos del fichero 'model.txt'
ipath = os.path.join('Datos', 'model.txt')
model = pd.read_csv(ipath, sep = "\s*", skiprows = 3,
parse_dates = {'Timestamp': [0, 1]}, index_col = 'Timestamp')
In [ ]:
for c in ['x1','x2','x3','x4','x5']: # Eliminamos unas columnas innecesarias
_ = wind.pop(c)
wind.head(3)
In [ ]:
model.head(3)
In [ ]:
wind['Timestamp'] = wind.index
print(wind['Timestamp'].diff().min())
del wind['Timestamp']
In [ ]:
model['Timestamp'] = model.index
print(model['Timestamp'].diff().min())
del model['Timestamp']
In [ ]:
wind.loc[:, 'wspd':'wspd_max'].plot(figsize = (15, 6))
In [ ]:
wind.loc['2014/01/01 00:00': '2014/01/01 02:00', 'wspd':'wspd_max'].plot(kind = 'bar', figsize = (15, 6))
In [ ]:
wind.loc['2014/01/01': '2014/12/31', 'wspd':'wspd_max'].plot(
kind = 'scatter',
x = 'wspd',
y = 'wspd_max',
figsize = (15, 6)
)
In [ ]:
wind.loc['2014/01/01': '2014/12/31', 'wspd':'wspd_max'].plot(
kind = 'scatter',
x = 'wspd',
y = 'wspd_max',
figsize = (15, 6),
xlim = (0, 32),
ylim = (0, 40)
)
In [ ]:
wind.loc['2014/01/01 00:00': '2014/01/01 02:00', 'wspd':'wspd_max'].plot(
kind = 'bar',
figsize = (15, 6),
subplots = True
)
Otra forma de hacerlo, plot
nos provee además de una API para separar los tipos de gráfico:
In [ ]:
s1 = wind.loc['2014/01/01': '2014/01/05', 'wspd']
s2 = s1.rolling(13, center = True).mean()
tmp = pd.DataFrame({'wspd': s1, 'mov_avg': s2})
tmp.plot.line(figsize = (15, 6))
Pero que pasa si quiero resaltar la línea que corresponde a 'mov_avg'
. Lo podemos hacer de la siguiente forma:
In [ ]:
ax = tmp['wspd'].plot.line(figsize = (15, 6))
tmp['mov_avg'].plot.line(figsize = (15, 6), lw = 5, ax = ax)
Otros tipos de gráficos:
In [ ]:
possible = dir(pd.DataFrame.plot)
for p in possible:
if '_' not in p: print(p)
Veamos, por ejemplo, el histograma de viento:
In [ ]:
wind['wspd'].plot.hist(bins = np.arange(0, 50), figsize = (15, 6))
Además, tenemos un submódulo llamado plotting
con herramientas de visualización un poco más avanzadas.
In [ ]:
pd.tools.plotting.scatter_matrix(
wind.iloc[0:1000, :],
diagonal = 'kde',
figsize = (12, 12)
)
In [ ]:
# Realizad vuestros propios gráficos con tipos de gráficos diferentes a los que hemos visto